package com.biubiushop.util;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.biubiushop.constant.ALiInfoConstant;

import cn.hutool.core.util.StrUtil;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * oss文件上传工具类
 * 
 * @author jc
 *
 */
public class OSSUtil {

	public static String uploadFile(MultipartFile file, String path) {
		// 判断文件
		if (file == null) {
			return null;
		}

		String filename = file.getOriginalFilename();
		String prefix = "";
		if (StrUtil.isEmpty(filename)) {
			prefix = prefix + ".png";
		} else {
			prefix = filename.substring(filename.lastIndexOf("."));
		}
		// 用uuid作为文件名，防止生成的临时文件重复
		File uploadFile = null;
		try {
			uploadFile = File.createTempFile(String.valueOf((int) (Math.random() * 900 + 100)), prefix);
			// MultipartFile to File
			file.transferTo(uploadFile);
		} catch (IllegalStateException e1) {
			e1.printStackTrace();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		// 创建OSSClient实例。
		OSS ossClient = new OSSClientBuilder().build(ALiInfoConstant.ENDPOINT, ALiInfoConstant.ACCESSKEYID, ALiInfoConstant.ACCESSKEYSECRET);
		// 设置文件路径和名称
		String fileUrl = "";
		try {
			// 判断容器是否存在,不存在就创建
			if (!ossClient.doesBucketExist(ALiInfoConstant.BUCKET_NAME)) {
				ossClient.createBucket(ALiInfoConstant.BUCKET_NAME);
				CreateBucketRequest createBucketRequest = new CreateBucketRequest(ALiInfoConstant.BUCKET_NAME);
				createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
				ossClient.createBucket(createBucketRequest);
			}
			fileUrl = path + "/" + uploadFile.getName();
			boolean flag = false;
			do {
				ossClient.putObject(new PutObjectRequest(ALiInfoConstant.BUCKET_NAME, fileUrl, uploadFile));
				// 判断文件是否存在
				flag = ossClient.doesObjectExist(ALiInfoConstant.BUCKET_NAME, fileUrl);
			} while (!flag);
		} catch (OSSException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		} finally {
			// 关闭OSSClient。
			ossClient.shutdown();
			deleteFile(uploadFile);
		}
		return "/" + fileUrl;

	}

	public static String uploadFile(File file, String path) {
		// 判断文件
		if (file == null) {
			return null;
		}

		// 创建OSSClient实例。
		OSS ossClient = new OSSClientBuilder().build(ALiInfoConstant.ENDPOINT, ALiInfoConstant.ACCESSKEYID, ALiInfoConstant.ACCESSKEYSECRET);
		// 设置文件路径和名称
		String fileUrl = "";
		try {
			// 判断容器是否存在,不存在就创建
			if (!ossClient.doesBucketExist(ALiInfoConstant.BUCKET_NAME)) {
				ossClient.createBucket(ALiInfoConstant.BUCKET_NAME);
				CreateBucketRequest createBucketRequest = new CreateBucketRequest(ALiInfoConstant.BUCKET_NAME);
				createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
				ossClient.createBucket(createBucketRequest);
			}
			fileUrl = path + "/" + file.getName();
			boolean flag = false;
			do {
				ossClient.putObject(new PutObjectRequest(ALiInfoConstant.BUCKET_NAME, fileUrl, file));
				// 判断文件是否存在
				flag = ossClient.doesObjectExist(ALiInfoConstant.BUCKET_NAME, fileUrl);
			} while (!flag);
		} catch (OSSException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		} finally {
			// 关闭OSSClient。
			ossClient.shutdown();
			deleteFile(file);
		}
		return "/" + fileUrl;
	}
	
	

	public static void deleteFile(File... files) {
		for (File file : files) {
			if (file.exists()) {
				file.delete();
			}
		}
	}

}
